package cn.pzaily.fealetter.service.impl;

import cn.pzaily.crab.tools.core.utils.ObjectUtils;
import cn.pzaily.fealetter.entity.PlatformEntity;
import cn.pzaily.fealetter.enumeration.RedisPropertiesKey;
import cn.pzaily.fealetter.mapper.PlatformMapper;
import cn.pzaily.fealetter.service.PlatformService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @PackageName: cn.pzaily.fealetter.service.impl
 * @Description:
 * @Author: Pang
 * @Date: 2021-11-21 18:03
 */
@Service
public class PlatformServiceImpl extends
        ServiceImpl<PlatformMapper, PlatformEntity> implements PlatformService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PlatformEntity findPlatformEntityByAccessKeyId(String accessKeyId) {

        ValueOperations<String, PlatformEntity> operations = redisTemplate.opsForValue();

        PlatformEntity platformEntity = operations.get(RedisPropertiesKey.PLATFORM_ACCESSKEYID + accessKeyId);

        if (ObjectUtils.isNull(platformEntity)) {
            platformEntity = baseMapper.selectOne(
                    new LambdaQueryWrapper<PlatformEntity>().eq(
                            PlatformEntity::getAccessKeyId, accessKeyId
                    )
            );
            if (ObjectUtils.isNull(platformEntity)) {
                redisTemplate.opsForValue().set(RedisPropertiesKey.PLATFORM_ACCESSKEYID + platformEntity.getAccessKeyId(),
                        platformEntity, RedisPropertiesKey.TIME, RedisPropertiesKey.TIME_UNIT);
            }
        }

        return platformEntity;
    }
}
